package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.service.ILogActionService;
import com.mdd.admin.validate.LogActionCreateValidate;
import com.mdd.admin.validate.LogActionSearchValidate;
import com.mdd.admin.validate.LogActionUpdateValidate;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.vo.LogActionDetailVo;
import com.mdd.admin.vo.LogActionListedVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.LogAction;
import com.mdd.common.mapper.LogActionMapper;
import com.mdd.common.util.StringUtils;
import com.mdd.common.util.TimeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * 操作日志实现类
 * @author LikeAdmin
 */
@Service
public class LogActionServiceImpl implements ILogActionService {
        
    @Resource
    LogActionMapper logActionMapper;

    /**
     * 操作日志列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<LogActionListedVo>
     */
    @Override
    public PageResult<LogActionListedVo> list(PageValidate pageValidate, LogActionSearchValidate searchValidate) throws ParseException {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<LogAction> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        logActionMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "like:uid:long",
            "like:account:str",
            "like:ip:str",
            "like:intro:str",
        });

        if (StringUtils.isNotEmpty(searchValidate.getCreateTimeStart()) && StringUtils.isNotEmpty(searchValidate.getCreateTimeEnd())) {
            Date start = new SimpleDateFormat("yyyy-MM-dd").parse(searchValidate.getCreateTimeStart());
            Date end = new SimpleDateFormat("yyyy-MM-dd").parse(searchValidate.getCreateTimeEnd());
            long startTime = start.getTime()/1000;
            long endTime = end.getTime()/1000;
            queryWrapper.between("create_time",startTime,endTime);
        }

        IPage<LogAction> iPage = logActionMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<LogActionListedVo> list = new LinkedList<>();
        for(LogAction item : iPage.getRecords()) {
            LogActionListedVo vo = new LogActionListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            vo.setAname("总平台");
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 操作日志详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return LogAction
     */
    @Override
    public LogActionDetailVo detail(Integer id) {
        LogAction model = logActionMapper.selectOne(
                new QueryWrapper<LogAction>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        LogActionDetailVo vo = new LogActionDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 操作日志新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(LogActionCreateValidate createValidate) {
        LogAction model = new LogAction();
        model.setAId(createValidate.getAId());
        model.setModule(createValidate.getModule());
        model.setController(createValidate.getController());
        model.setAction(createValidate.getAction());
        model.setIntro(createValidate.getIntro());
        model.setUid(createValidate.getUid());
        model.setAccount(createValidate.getAccount());
        model.setSys_aid(createValidate.getSys_aid());
        model.setIp(createValidate.getIp());
        model.setParams(createValidate.getParams());
        model.setResult(createValidate.getResult());
        model.setCreateTime(System.currentTimeMillis() / 1000);
        logActionMapper.insert(model);
    }

    /**
     * 操作日志编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(LogActionUpdateValidate updateValidate) {
        LogAction model = logActionMapper.selectOne(
                new QueryWrapper<LogAction>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setAId(updateValidate.getAId());
        model.setModule(updateValidate.getModule());
        model.setController(updateValidate.getController());
        model.setAction(updateValidate.getAction());
        model.setIntro(updateValidate.getIntro());
        model.setUid(updateValidate.getUid());
        model.setAccount(updateValidate.getAccount());
        model.setSys_aid(updateValidate.getSys_aid());
        model.setIp(updateValidate.getIp());
        model.setParams(updateValidate.getParams());
        model.setResult(updateValidate.getResult());
        logActionMapper.updateById(model);
    }

    /**
     * 操作日志删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        LogAction model = logActionMapper.selectOne(
                new QueryWrapper<LogAction>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        logActionMapper.delete(new QueryWrapper<LogAction>().eq("id", id));
    }

}
